package handler

import (
	"encoding/json"
	"hema-fresh/cache"
	"hema-fresh/moder"
	"hema-fresh/pkg"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func UserLogin(c *gin.Context) {
	Username := c.PostForm("Username")

	Password := c.PostForm("Password")
	if Username == "" || Password == "" {
		c.JSON(200, gin.H{
			"mas":  "不能为空",
			"code": 500,
		})
		return
	}

	redis_key := "redis_login" + Username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"mas":  "密码错误3次",
			"code": 500,
		})
		return
	}

	var user moder.User
	err := moder.DB.Where("username=?", Username).Find(user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"mas":  "查询失败",
			"code": 500,
		})
		return

	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"mas":  "用户名不存在",
			"code": 500,
		})
		return
	}

	if user.Password != pkg.MD5(Password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"mas":  "密码错误3次，封禁2小时",
				"code": 500,
			})
			return
		}

		c.JSON(200, gin.H{
			"mas":  "密码错误",
			"code": 500,
		})
		return
	}

	c.JSON(200, gin.H{
		"mas":  "密码正确",
		"code": 200,
	})
	return

}

func RodeNodeAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := moder.RodeNode{
		Rid: rid,
		Nid: nid,
	}
	moder.DB.Create(&data)
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
		"data": data,
	})
	return
}

func GoodsAdd(c *gin.Context) {
	GoodsNo := c.PostForm("GoodsNo")
	GoodsName := c.PostForm("GoodsName")
	data := moder.Goods{
		GoodsNo:   GoodsNo,
		GoodsName: GoodsName,
	}
	moder.DB.Create(&data)
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
	})
	return
}

func OrderAdd(c *gin.Context) {
	OrderNo := c.PostForm("OrderNo")
	OrderName := c.PostForm("OrderName")
	OrderPrice := c.PostForm("OrderPrice")
	Gid, _ := strconv.Atoi(c.PostForm("Gid"))
	order := moder.Order{
		OrderNo:    OrderNo,
		OrderName:  OrderName,
		OrderPrice: OrderPrice,
		Gid:        Gid,
	}
	moder.DB.Create(&order)
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
	})
	return
}

func GoodsOrder(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	/*
		SELECT
		  goods.id AS ID,
		  goods.goods_no AS GoodsNo,
		  goods.goods_name AS GoodsName,
		  orders.order_no AS OrderNo,
		  orders.order_name AS OrderName,
		  orders.order_price AS OrderPrice
		FROM
		  goods
		  LEFT JOIN orders ON orders.gid = goods.id
	*/
	var data []moder.GoodsOrder

	redis_key := "redis_list" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		moder.DB.Table("goods").
			Select(
				"goods.id AS ID",
				" goods.goods_no AS GoodsNo",
				"goods.goods_name AS GoodsName",
				"orders.order_no AS OrderNo",
				"orders.order_name AS OrderName",
				"orders.order_price AS OrderPrice").
			Joins("LEFT JOIN orders ON orders.gid = goods.id").
			Offset(offset).
			Limit(size).
			Find(&data)
		jsonData, _ := json.Marshal(&data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &data)
	}

	var total int64
	moder.DB.Model(&moder.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"mas":   "成功",
		"code":  200,
		"data":  data,
		"total": total,
	})
	return
}

func Detail(c *gin.Context) {
	id := c.Query("id")
	var data moder.Order
	redis_key := "redis_detail" + id
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		moder.DB.Where("id=?", id).First(&data)
		jsonData, _ := json.Marshal(&data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &data)
	}

	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
		"data": data,
	})

}
